package service;

import JDBC.DBUtils;
import method.ItemInformation;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * @author Gao Shiwei
 * user can get one item's concrete information from here
 */
@WebServlet(name = "OneItem")
public class OneItem extends HttpServlet {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
            resp.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, If-Modified-Since");
            resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            resp.setHeader("Access-Control-Max-Age", "3600");
            resp.addHeader("Access-Control-Allow-Credentials", "true");
            String ItemID=req.getParameter("ItemID");
            ItemInformation[] Item=DBUtils.QueryItemInformation("ItemID",ItemID);
            String ItemName= Item[0].getItemName();
            System.out.println(ItemName);
            double ItemPrice=Item[0].getItemPrice();
            String ItemDescription=Item[0].getItemDescription();
            int ItemQuantity=Item[0].getItemQuantity();
            String ItemType=Item[0].getItemType();
            String picture=DBUtils.QueryItemPicture(ItemID);
            if(picture!=null)
                picture=picture.replace("\\","\\\\");
            String answer="{\"ItemName\":\""+ItemName+"\","+
                    "\"ItemPrice\":"+ItemPrice+","+
                    "\"ItemDescription\":\""+ItemDescription+"\","+
                    "\"ItemQuantity\":"+ItemQuantity+","+
                    "\"ItemType\":\""+ItemType+"\","+
                    "\"ItemPicture\":\""+picture+"\""+
                    "}";
            PrintWriter out = resp.getWriter();
            out.println(answer);
            out.flush();
            out.close();
        }
    }